package com.atguigu.myssm.listener;

import com.atguigu.myssm.ioc.BeanFactory;
import com.atguigu.myssm.ioc.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * 监听上下文启动, 在上下文启动时创建IOC容器, 然后将其保存到application作用域
 * 后面中央控制器DispatcherServlet从application获取IOC容器
 */
@WebListener
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // 获取servlet上下文对象
        ServletContext application = servletContextEvent.getServletContext();
        // 获取上下文的初始化参数
        String path = application.getInitParameter("contextConfigLocation");
        // 创建IOC容器
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(path);
        // 将IOC容器保存到application作用域
        application.setAttribute("beanFactory", beanFactory);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
